<?php

class Helper
{
    // 运行
    static function run($event_name, $paramters = [])
    {
        $e = new self();
        if (method_exists($e, $event_name)) {
            return $e->$event_name($paramters);
        } else {
            throw new Exception('event not exist');
            return false;
        }
    }

    // 获取IP地址
    static function ip()
    {
        $_ip = '0.0.0.0';
        if ($_SERVER) {
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
                $_ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
            }
            if ($_ip == '127.0.0.1') {
                if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                    $_ip = $_SERVER["HTTP_CLIENT_IP"];
                } else if (isset($_SERVER["REMOTE_ADDR"])) {
                    $_ip = $_SERVER["REMOTE_ADDR"];
                }
            }
        } else {
            if (getenv('HTTP_X_FORWARDED_FOR')) {
                $_ip = getenv('HTTP_X_FORWARDED_FOR');
            } else if (getenv('HTTP_CLIENT_IP')) {
                $_ip = getenv('HTTP_CLIENT_IP');
            } else if (getenv('REMOTE_ADDR')) {
                $_ip = getenv('REMOTE_ADDR');
            }
        }
        if (!self::is_ip($_ip)) $_ip = '0.0.0.0';
        $_ips = explode(',', $_ip);
        if (count($_ips) > 1) return $_ips[0];
        return $_ip;
    }

    static function is_ip($gonten)
    {
        $_ip = explode(',', $gonten);
        $ip = explode(".", $_ip[0]);
        for ($i = 0; $i < count($ip); $i++) {
            if ($ip[$i] > 255) {
                return false;
            }
        }
        return preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_ip[0]);
    }

    /**
     * [getPassedHours 某时间戳到现在所经过的时间]
     * @param  [int] $distence [时间戳]
     * @return [string]           [秒/分钟/小时]
     */
    static function getPassedHours($distence)
    {
        $passed = "";
        switch ($distence) {
            case ($distence < 60) :
                {
                    $passed = $distence . "秒";
                    break;
                }
            case ($distence > 60 && $distence < 60 * 60) :
                {
                    $passed = intval($distence / 60) . "分钟";
                    break;
                }
            case ($distence > 60 * 60) :
                {
                    $passed = sprintf("%.1f", $distence / (60 * 60)) . "小时";
                    break;
                }
        }

        return $passed;
    }

    static function transCase($str)
    {
        $str = preg_replace('/(e|ｅ|Ｅ)(x|ｘ|Ｘ)(p|ｐ|Ｐ)(r|ｒ|Ｒ)(e|ｅ|Ｅ)(s|ｓ|Ｓ)(s|ｓ|Ｓ)(i|ｉ|Ｉ)(o|ｏ|Ｏ)(n|ｎ|Ｎ)/is', 'expression', $str);
        Return $str;
    }

    // 过滤xss
    static function removeXSS($str)
    {
        return htmlspecialchars($str);
    }


    /**
     * 简单的订单号hash加解密
     * @param $number_string
     * @param string $type
     * @return string
     */
    static function hash_orderid($number_string, $type = 'encode')
    {
        $rand_array = array(
            '0' => 'a',
            '1' => 'g',
            '2' => 'd',
            '3' => 'o',
            '4' => '0',
            '5' => 'n',
            '6' => '1',
            '7' => '2',
            '8' => 'z',
            '9' => '3',
            'a' => '4',
            'b' => 'k',
            'c' => 'y',
            'd' => 'j',
            'e' => 'f',
            'f' => 'm',
            'g' => 'b',
            'h' => 'x',
            'i' => 'w',
            'j' => '6',
            'k' => '5',
            'l' => 'i',
            'm' => 'c',
            'n' => '7',
            'o' => 'p',
            'p' => '8',
            'q' => 'q',
            'r' => 'h',
            's' => 'e',
            't' => 'r',
            'u' => 't',
            'v' => 'u',
            'w' => 'l',
            'x' => 's',
            'y' => '9',
            'z' => 'v',
        );
        if ($type == 'decode') {
            $rand_array = array_flip($rand_array);
        }
        $string = "$number_string";
        $new_string = '';
        for ($i = 0; $i < strlen($string); $i++) {
            $new_string .= $rand_array["{$string[$i]}"];
        }
        return $new_string;
    }

    static function result($status = true, $msg = 'ok', $data = [])
    {
        return array_merge([
            'status' => $status,
            'msg' => $msg
        ], $data);
    }
}
